<!doctype html>














<!-- `site.alt_lang` can specify a language different from the UI -->
<html lang="en" 
  
>
  <!-- The Head -->

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta name="theme-color" media="(prefers-color-scheme: light)" content="#f7f7f7">
  <meta name="theme-color" media="(prefers-color-scheme: dark)" content="#1b1b1e">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
  <meta
    name="viewport"
    content="width=device-width, user-scalable=no initial-scale=1, shrink-to-fit=no, viewport-fit=cover"
  >

  

  

  
    <!-- Begin Jekyll SEO tag v2.8.0 -->
<meta name="generator" content="Jekyll v4.3.2" />
<meta property="og:title" content="Direct memory access(DMA)" />
<meta name="author" content="Chunsheng Luo" />
<meta property="og:locale" content="en" />
<meta name="description" content="一、DMA" />
<meta property="og:description" content="一、DMA" />
<link rel="canonical" href="https://luochenglcs.github.io/posts/dma-and-iommu/" />
<meta property="og:url" content="https://luochenglcs.github.io/posts/dma-and-iommu/" />
<meta property="og:site_name" content="Cheng Luo" />
<meta property="og:type" content="article" />
<meta property="article:published_time" content="2021-10-14T00:00:00+00:00" />
<meta name="twitter:card" content="summary" />
<meta property="twitter:title" content="Direct memory access(DMA)" />
<meta name="twitter:site" content="@luochenglcs" />
<meta name="twitter:creator" content="@Chunsheng Luo" />
<script type="application/ld+json">
{"@context":"https://schema.org","@type":"BlogPosting","author":{"@type":"Person","name":"Chunsheng Luo","url":"https://github.com/luochenglcs"},"dateModified":"2023-08-08T01:08:45+00:00","datePublished":"2021-10-14T00:00:00+00:00","description":"一、DMA","headline":"Direct memory access(DMA)","mainEntityOfPage":{"@type":"WebPage","@id":"https://luochenglcs.github.io/posts/dma-and-iommu/"},"url":"https://luochenglcs.github.io/posts/dma-and-iommu/"}</script>
<!-- End Jekyll SEO tag -->

  

  <title>Direct memory access(DMA) | Cheng Luo
  </title>

  <!--
  The Favicons for Web, Android, Microsoft, and iOS (iPhone and iPad) Apps
  Generated by: https://realfavicongenerator.net/
-->



<link rel="apple-touch-icon" sizes="180x180" href="/assets/img/favicons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/assets/img/favicons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/assets/img/favicons/favicon-16x16.png">
<link rel="manifest" href="/assets/img/favicons/site.webmanifest">
<link rel="shortcut icon" href="/assets/img/favicons/favicon.ico">
<meta name="apple-mobile-web-app-title" content="Cheng Luo">
<meta name="application-name" content="Cheng Luo">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="msapplication-config" content="/assets/img/favicons/browserconfig.xml">
<meta name="theme-color" content="#ffffff">


  
    
      <link rel="preconnect" href="https://fonts.googleapis.com" >
      <link rel="dns-prefetch" href="https://fonts.googleapis.com" >
    
      <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
      <link rel="dns-prefetch" href="https://fonts.gstatic.com" crossorigin>
    
      <link rel="preconnect" href="https://fonts.googleapis.com" >
      <link rel="dns-prefetch" href="https://fonts.googleapis.com" >
    
      <link rel="preconnect" href="https://cdn.jsdelivr.net" >
      <link rel="dns-prefetch" href="https://cdn.jsdelivr.net" >
    

    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Lato&family=Source+Sans+Pro:wght@400;600;700;900&display=swap">
  

  <!-- GA -->
  

  <!-- Bootstrap -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">

  <!-- Font Awesome -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.4.0/css/all.min.css">

  <link rel="stylesheet" href="/assets/css/style.css">

  
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/tocbot@4.21.0/dist/tocbot.min.css">
  

  
    <!-- Manific Popup -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/magnific-popup@1.1.0/dist/magnific-popup.min.css">
  

  <!-- JavaScript -->

  
    <!-- Switch the mode between dark and light. -->

<script type="text/javascript">
  class ModeToggle {
    static get MODE_KEY() {
      return 'mode';
    }
    static get MODE_ATTR() {
      return 'data-mode';
    }
    static get DARK_MODE() {
      return 'dark';
    }
    static get LIGHT_MODE() {
      return 'light';
    }
    static get ID() {
      return 'mode-toggle';
    }

    constructor() {
      if (this.hasMode) {
        if (this.isDarkMode) {
          if (!this.isSysDarkPrefer) {
            this.setDark();
          }
        } else {
          if (this.isSysDarkPrefer) {
            this.setLight();
          }
        }
      }

      let self = this;

      /* always follow the system prefers */
      this.sysDarkPrefers.addEventListener('change', () => {
        if (self.hasMode) {
          if (self.isDarkMode) {
            if (!self.isSysDarkPrefer) {
              self.setDark();
            }
          } else {
            if (self.isSysDarkPrefer) {
              self.setLight();
            }
          }

          self.clearMode();
        }

        self.notify();
      });
    } /* constructor() */

    get sysDarkPrefers() {
      return window.matchMedia('(prefers-color-scheme: dark)');
    }

    get isSysDarkPrefer() {
      return this.sysDarkPrefers.matches;
    }

    get isDarkMode() {
      return this.mode === ModeToggle.DARK_MODE;
    }

    get isLightMode() {
      return this.mode === ModeToggle.LIGHT_MODE;
    }

    get hasMode() {
      return this.mode != null;
    }

    get mode() {
      return sessionStorage.getItem(ModeToggle.MODE_KEY);
    }

    /* get the current mode on screen */
    get modeStatus() {
      if (this.isDarkMode || (!this.hasMode && this.isSysDarkPrefer)) {
        return ModeToggle.DARK_MODE;
      } else {
        return ModeToggle.LIGHT_MODE;
      }
    }

    setDark() {
      document.documentElement.setAttribute(ModeToggle.MODE_ATTR, ModeToggle.DARK_MODE);
      sessionStorage.setItem(ModeToggle.MODE_KEY, ModeToggle.DARK_MODE);
    }

    setLight() {
      document.documentElement.setAttribute(ModeToggle.MODE_ATTR, ModeToggle.LIGHT_MODE);
      sessionStorage.setItem(ModeToggle.MODE_KEY, ModeToggle.LIGHT_MODE);
    }

    clearMode() {
      document.documentElement.removeAttribute(ModeToggle.MODE_ATTR);
      sessionStorage.removeItem(ModeToggle.MODE_KEY);
    }

    /* Notify another plugins that the theme mode has changed */
    notify() {
      window.postMessage(
        {
          direction: ModeToggle.ID,
          message: this.modeStatus
        },
        '*'
      );
    }

    flipMode() {
      if (this.hasMode) {
        if (this.isSysDarkPrefer) {
          if (this.isLightMode) {
            this.clearMode();
          } else {
            this.setLight();
          }
        } else {
          if (this.isDarkMode) {
            this.clearMode();
          } else {
            this.setDark();
          }
        }
      } else {
        if (this.isSysDarkPrefer) {
          this.setLight();
        } else {
          this.setDark();
        }
      }

      this.notify();
    } /* flipMode() */
  } /* ModeToggle */

  const modeToggle = new ModeToggle();
</script>

  

  <!-- A placeholder to allow defining custom metadata -->

</head>


  <body>
    <!-- The Side Bar -->

<div id="sidebar" class="d-flex flex-column align-items-end">
  <div class="profile-wrapper">
    <a href="/" id="avatar" class="rounded-circle">
      
        
        <img src="/assets/img/favicons/android-chrome-512x512.png" width="112" height="112" alt="avatar" onerror="this.style.display='none'">
      
    </a>

    <div class="site-title">
      <a href="/">Cheng Luo</a>
    </div>
    <div class="site-subtitle fst-italic">Linux operating system engineer</div>
  </div>
  <!-- .profile-wrapper -->

  <ul class="nav flex-column flex-grow-1 w-100 ps-0">
    <!-- home -->
    <li class="nav-item">
      <a href="/" class="nav-link">
        <i class="fa-fw fas fa-home"></i>
        <span>HOME</span>
      </a>
    </li>
    <!-- the real tabs -->
    
      <li class="nav-item">
        <a href="/categories/" class="nav-link">
          <i class="fa-fw fas fa-stream"></i>
          

          <span>CATEGORIES</span>
        </a>
      </li>
      <!-- .nav-item -->
    
      <li class="nav-item">
        <a href="/tags/" class="nav-link">
          <i class="fa-fw fas fa-tags"></i>
          

          <span>TAGS</span>
        </a>
      </li>
      <!-- .nav-item -->
    
      <li class="nav-item">
        <a href="/archives/" class="nav-link">
          <i class="fa-fw fas fa-archive"></i>
          

          <span>ARCHIVES</span>
        </a>
      </li>
      <!-- .nav-item -->
    
      <li class="nav-item">
        <a href="/about/" class="nav-link">
          <i class="fa-fw fas fa-info-circle"></i>
          

          <span>ABOUT</span>
        </a>
      </li>
      <!-- .nav-item -->
    
  </ul>
  <!-- ul.nav.flex-column -->

  <div class="sidebar-bottom d-flex flex-wrap  align-items-center w-100">
    
      <button class="mode-toggle btn" aria-label="Switch Mode">
        <i class="fas fa-adjust"></i>
      </button>

      
        <span class="icon-border"></span>
      
    

    
      

      
        <a
          href="https://github.com/luochenglcs"
          aria-label="github"
          

          
            target="_blank"
            
          

          

          
            rel="noopener noreferrer"
          
        >
          <i class="fab fa-github"></i>
        </a>
      
    
      

      
    
      

      
        <a
          href="javascript:location.href = 'mailto:' + ['luochunsheng','ustc.edu'].join('@')"
          aria-label="email"
          

          

          

          
        >
          <i class="fas fa-envelope"></i>
        </a>
      
    
  </div>
  <!-- .sidebar-bottom -->
</div>
<!-- #sidebar -->


    <div id="main-wrapper" class="d-flex justify-content-center">
      <div id="main" class="container px-xxl-5">
        <!-- The Top Bar -->

<div id="topbar-wrapper">
  <div
    id="topbar"
    class="container d-flex align-items-center justify-content-between h-100"
  >
    <span id="breadcrumb">
      

      
        
          
            <span>
              <a href="/">
                Home
              </a>
            </span>

          
        
          
        
          
            
              <span>Direct memory access(DMA)</span>
            

          
        
      
    </span>
    <!-- endof #breadcrumb -->

    <i id="sidebar-trigger" class="fas fa-bars fa-fw"></i>

    <div id="topbar-title">
      Post
    </div>

    <i id="search-trigger" class="fas fa-search fa-fw"></i>
    <span id="search-wrapper" class="align-items-center">
      <i class="fas fa-search fa-fw"></i>
      <input
        class="form-control"
        id="search-input"
        type="search"
        aria-label="search"
        autocomplete="off"
        placeholder="Search..."
      >
    </span>
    <span id="search-cancel">Cancel</span>
  </div>
</div>

        











<div class="row">
  <!-- core -->
  <div id="core-wrapper" class="col-12 col-lg-11 col-xl-9 pe-xl-4">
    

    <div class="post px-1 px-md-2">
      

      
        
      
        <!-- Refactor the HTML structure -->



<!--
  In order to allow a wide table to scroll horizontally,
  we suround the markdown table with `<div class="table-wrapper">` and `</div>`
-->



<!--
  Fixed kramdown code highlight rendering:
  https://github.com/penibelst/jekyll-compress-html/issues/101
  https://github.com/penibelst/jekyll-compress-html/issues/71#issuecomment-188144901
-->



<!-- Change the icon of checkbox -->


<!-- images -->



  
  

  <!-- CDN URL -->
  

  <!-- Add image path -->
  

  
    
      
      
    

    
    

    

    
    

    
    
    
    

    
      

      
      
      

      
    
      

      
      
      

      
    

    <!-- take out classes -->
    

    

    

    

    <!-- lazy-load images <https://github.com/aFarkas/lazysizes#readme> -->
    
    

    <!-- add image placeholder -->
    
      
    

    <!-- Bypass the HTML-proofer test -->
    

    
      <!-- make sure the `<img>` is wrapped by `<a>` -->
      

      
        <!-- create the image wrapper -->
        
      
    

    <!-- combine -->
    

  
    

    
    

    

    
    

    
    
    
    

    
      

      
      
      

      
    
      

      
      
      

      
    

    <!-- take out classes -->
    

    

    

    

    <!-- lazy-load images <https://github.com/aFarkas/lazysizes#readme> -->
    
    

    <!-- add image placeholder -->
    
      
    

    <!-- Bypass the HTML-proofer test -->
    

    
      <!-- make sure the `<img>` is wrapped by `<a>` -->
      

      
        <!-- create the image wrapper -->
        
      
    

    <!-- combine -->
    

  
    

    
    

    

    
    

    
    
    
    

    
      

      
      
      

      
    
      

      
      
      

      
    

    <!-- take out classes -->
    

    

    

    

    <!-- lazy-load images <https://github.com/aFarkas/lazysizes#readme> -->
    
    

    <!-- add image placeholder -->
    
      
    

    <!-- Bypass the HTML-proofer test -->
    

    
      <!-- make sure the `<img>` is wrapped by `<a>` -->
      

      
        <!-- create the image wrapper -->
        
      
    

    <!-- combine -->
    

  
    

    
    

    

    
    

    
    
    
    

    
      

      
      
      

      
    
      

      
      
      

      
    

    <!-- take out classes -->
    

    

    

    

    <!-- lazy-load images <https://github.com/aFarkas/lazysizes#readme> -->
    
    

    <!-- add image placeholder -->
    
      
    

    <!-- Bypass the HTML-proofer test -->
    

    
      <!-- make sure the `<img>` is wrapped by `<a>` -->
      

      
        <!-- create the image wrapper -->
        
      
    

    <!-- combine -->
    

  

  



<!-- Add header for code snippets -->



<!-- Create heading anchors -->





  
  

  

  
  

  
    
    

    
      
        
        
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    

    

  

  
  

  
    
    

    
      
        
        
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    

    

  

  
  

  
    
    

    
      
        
        
      

      
      

      
      
      

      

    

    

  




<!-- return -->




<h1 data-toc-skip>Direct memory access(DMA)</h1>

<div class="post-meta text-muted">
    <!-- published date -->
    <span>
      Posted
      <!--
  Date format snippet
  See: ${JS_ROOT}/utils/locale-dateime.js
-->





<em
  class=""
  data-ts="1634169600"
  data-df="ll"
  
    data-bs-toggle="tooltip" data-bs-placement="bottom"
  
>
  Oct 14, 2021
</em>

    </span>

    <!-- lastmod date -->
    
    <span>
      Updated
      <!--
  Date format snippet
  See: ${JS_ROOT}/utils/locale-dateime.js
-->





<em
  class=""
  data-ts="1691456925"
  data-df="ll"
  
    data-bs-toggle="tooltip" data-bs-placement="bottom"
  
>
  Aug  8, 2023
</em>

    </span>
    

  

  <div class="d-flex justify-content-between">
    <!-- author(s) -->
    <span>
      

      By

      <em>
      
        
          <a href="https://github.com/luochenglcs">Chunsheng Luo</a>
          
        
      
      </em>
    </span>

    <div>
      <!-- read time -->
      <!-- Calculate the post's reading time, and display the word count in tooltip -->



<!-- words per minute -->










<!-- return element -->
<span
  class="readtime"
  data-bs-toggle="tooltip"
  data-bs-placement="bottom"
  title="1584 words"
>
  <em>8 min</em> read</span>

    </div>

  </div> <!-- .d-flex -->

</div> <!-- .post-meta -->
<div class="post-content">
  <h3 id="一dma"><span class="me-2">一、DMA</span><a href="#一dma" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h3>

<p>DMA，全称Direct Memory Access，即直接存储器访问。</p>

<p>DMA传输将数据从一个地址空间复制到另一个地址空间，提供在外设和存储器之间或者存储器和存储器之间的高速数据传输。当CPU初始化这个传输动作，传输动作本身是由DMA控制器来实现和完成的。DMA传输方式无需CPU直接控制传输，也没有中断处理方式那样保留现场和恢复现场过程，通过硬件为RAM和IO设备开辟一条直接传输数据的通道，使得CPU的效率大大提高.</p>

<p><a href="/posts/.\images\dma-and-iommu\DMA-TO-IOMMU示意图.png" class="popup img-link "><img data-src="/posts/.\images\dma-and-iommu\DMA-TO-IOMMU示意图.png" alt="image-20221130094857892" class="lazyload" data-proofer-ignore></a></p>

<p><a href="https://blog.csdn.net/as480133937/article/details/104927922">【STM32】 DMA原理，步骤超细详解，一文看懂DMA</a></p>

<p>看到STM32内核，存储器，外设及DMA的连接，这些硬件最终通过各种各样的线连接到总线矩阵中，硬件结构之间的数据转移都经过总线矩阵的协调，使各个外设和谐的使用总线来传输数据。</p>

<p><a href="/posts/.\images\dma-and-iommu\DMA框图.png" class="popup img-link "><img data-src="/posts/.\images\dma-and-iommu\DMA框图.png" alt="image-20221130160023106" class="lazyload" data-proofer-ignore></a></p>

<p>AHB介绍：</p>

<div class="language-plaintext highlighter-rouge"><div class="code-header">
        <span data-label-text="Plaintext"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre>  AHB总线规范是AMBA总线规范的一部分，AMBA总线规范是ARM公司提出的总线规范，被大多数SoC设计采用，它规定了AHB (Advanced High-performance Bus)、ASB (Advanced System Bus)、APB (Advanced Peripheral Bus)。AHB用于高性能、高时钟频率的系统结构，典型的应用如ARM核与系统内部的高速RAM、NAND FLASH、DMA、Bridge的连接。APB用于连接外部设备，对性能要求不高，而考虑低功耗问题。ASB是AHB的一种替代方案。

AHB总线的架构
        AHB总线的强大之处在于它可以将微控制器（CPU）、高带宽的片上RAM、高带宽的外部存储器接口、DMA总线master、各种拥有AHB接口的控制器等等连接起来构成一个独立的完整的SOC系统，不仅如此，还可以通过AHB-APB桥来连接APB总线系统。AHB可以成为一个完整独立的SOC芯片的骨架。  ![image-20221130161933206](.\images\dma-and-iommu\AHB总线.png)
</pre></td></tr></tbody></table></code></div></div>

<p><a href="https://zhuanlan.zhihu.com/p/138573828">DMA原理介绍</a></p>

<p><a href="/posts/.\images\dma-and-iommu\DMA硬件图.png" class="popup img-link "><img data-src="/posts/.\images\dma-and-iommu\DMA硬件图.png" alt="image-20221130155800244" class="lazyload" data-proofer-ignore></a></p>

<p><a href="/posts/.\images\dma-and-iommu\DMA port连接总线.png" class="popup img-link "><img data-src="/posts/.\images\dma-and-iommu\DMA port连接总线.png" alt="image-20221130160528249" class="lazyload" data-proofer-ignore></a></p>

<h3 id="二iommu"><span class="me-2">二、IOMMU</span><a href="#二iommu" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h3>

<p>1、DMA做地址翻译，用来解决DMA的安全性问题，DMA API同时肩负起了设置IOMMU的职责。</p>

<p>2、做设备虚拟化直通。</p>

<h3 id="三dma-iommu"><span class="me-2">三、DMA-IOMMU</span><a href="#三dma-iommu" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h3>

<p>IOMMU：DMA （iova）-&gt; memory port -&gt; iommu（pa） -&gt;  总线 -&gt; 内存控制器 存/取主存内容</p>

<p>代码分析：</p>

<p>设备attach iommu时install ops，</p>

<p>arm64: iommu_dma_ops</p>

<p>intel:  intel_dma_ops</p>

<h4 id="1dma-ops接口"><span class="me-2">1、dma ops接口</span><a href="#1dma-ops接口" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h4>

<div class="table-wrapper"><table>
  <thead>
    <tr>
      <th>dma_ops接口</th>
      <th>功能</th>
      <th>dma ops</th>
      <th>iommu ops</th>
      <th>iova分配</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>alloc(*dev, size, *dma_handle, gfp)</td>
      <td>申请物理页 + 申请iova + iommu mapm</td>
      <td> </td>
      <td>map</td>
      <td>Y</td>
    </tr>
    <tr>
      <td>free(*dev, size, *vaddr, dma_handle, attr)</td>
      <td>iommu unmap + 释放iova + 释放物理页</td>
      <td> </td>
      <td>unmap</td>
      <td>Y</td>
    </tr>
    <tr>
      <td>alloc_pages(*dev, size, *dma_handle, dir, gfp)</td>
      <td>申请dma连续物理页 + dma map_page</td>
      <td>map_page</td>
      <td> </td>
      <td>Y</td>
    </tr>
    <tr>
      <td>free_pages(*dev, size, *vaddr, dma_handle, dir)</td>
      <td>dma unmap_page + 释放连续物理页</td>
      <td>unmap_page</td>
      <td> </td>
      <td>Y</td>
    </tr>
    <tr>
      <td>alloc_noncoherent(*dev, size, *dma_handle, dir, gfp)</td>
      <td>与alloc_pages一致</td>
      <td>noblock: map_page</td>
      <td>block: map</td>
      <td>Y</td>
    </tr>
    <tr>
      <td>free_noncoherent(*dev, size, *vaddr, dma_handle, dir)</td>
      <td>与free_pages一致</td>
      <td>noblock: unmap_page</td>
      <td>block: unmap</td>
      <td>Y</td>
    </tr>
    <tr>
      <td>mmap</td>
      <td>mmu mmap</td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>get_sgtable</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>map_page(*dev, *page, offset, size, dir, attrs)</td>
      <td>申请iova + iommu map</td>
      <td> </td>
      <td>map</td>
      <td>Y</td>
    </tr>
    <tr>
      <td>unmap_page(*dev, dma_handle, size, dir, attrs)</td>
      <td>iommu unmap + 释放iova</td>
      <td> </td>
      <td>unmap</td>
      <td>Y</td>
    </tr>
    <tr>
      <td>map_sg(*dev, *sg, nents, dir, attrs)</td>
      <td>与map_page一致</td>
      <td> </td>
      <td>map</td>
      <td>Y</td>
    </tr>
    <tr>
      <td>unmap_sg(*dev, *sg, nents, dir, attrs)</td>
      <td>与unmap_page一致</td>
      <td> </td>
      <td>unmap</td>
      <td>Y</td>
    </tr>
    <tr>
      <td>map_resource(*dev, phys, size, dir, attrs)</td>
      <td>申请iova + iommu map</td>
      <td> </td>
      <td> </td>
      <td>Y</td>
    </tr>
    <tr>
      <td>unmap_resource(*dev, dma_handle, size, dir, attrs)</td>
      <td>iommu unmap + 释放iova</td>
      <td> </td>
      <td> </td>
      <td>Y</td>
    </tr>
    <tr>
      <td>sync_single_for_cpu</td>
      <td>iova_to_phys +  inv Dcache 保证cache一致性</td>
      <td> </td>
      <td>iova_to_phys</td>
      <td> </td>
    </tr>
    <tr>
      <td>sync_single_for_device</td>
      <td>iova_to_phys +  inv Dcache + clear Dcache 保证cache一致性</td>
      <td> </td>
      <td>iova_to_phys</td>
      <td> </td>
    </tr>
    <tr>
      <td>sync_sg_for_cpu</td>
      <td>与sync_single_for_cpu一致</td>
      <td> </td>
      <td>iova_to_phys</td>
      <td> </td>
    </tr>
    <tr>
      <td>sync_sg_for_device</td>
      <td>与sync_single_for_device一致</td>
      <td> </td>
      <td>iova_to_phys</td>
      <td> </td>
    </tr>
    <tr>
      <td>cache_sync</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>dma_supported</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>get_required_mask</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>max_mapping_size</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>get_merge_boundary</td>
      <td>获取iommu在使用的page size</td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
  </tbody>
</table></div>

<h4 id="2iommu-ops"><span class="me-2">2、iommu ops</span><a href="#2iommu-ops" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h4>

<div class="table-wrapper"><table>
  <thead>
    <tr>
      <th>iommu  ops接口</th>
      <th>功能</th>
      <th>分类</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>capable</td>
      <td>check capability</td>
      <td>iommu基础能力check</td>
    </tr>
    <tr>
      <td>domain_alloc</td>
      <td>allocate iommu domain</td>
      <td>管理dev集合</td>
    </tr>
    <tr>
      <td>domain_free</td>
      <td>free iommu domain</td>
      <td>管理dev集合</td>
    </tr>
    <tr>
      <td>attach_dev</td>
      <td>attach device to an  iommu domain</td>
      <td>管理dev集合</td>
    </tr>
    <tr>
      <td>detach_dev</td>
      <td>detach device from an  iommu domain</td>
      <td>管理dev集合</td>
    </tr>
    <tr>
      <td><font color="red"> map</font></td>
      <td>map a physically  contiguous memory region to an iommu domain</td>
      <td>iommu page table操作</td>
    </tr>
    <tr>
      <td><font color="red">unmap</font></td>
      <td>unmap a physically  contiguous memory region from an iommu domain</td>
      <td>iommu page table操作</td>
    </tr>
    <tr>
      <td>flush_iotlb_all</td>
      <td>Synchronously flush all  hardware TLBs for this domain</td>
      <td>iommu page table操作</td>
    </tr>
    <tr>
      <td>iotlb_sync_map</td>
      <td>Sync mappings created  recently using @map to the hardware</td>
      <td>iommu page table操作</td>
    </tr>
    <tr>
      <td>iotlb_sync</td>
      <td>Flush all queued ranges  from the hardware TLBs and empty flush queue</td>
      <td>iommu page table操作</td>
    </tr>
    <tr>
      <td><font color="red">iova_to_phys</font></td>
      <td>translate iova to  physical address</td>
      <td>iommu page table操作</td>
    </tr>
    <tr>
      <td>probe_device</td>
      <td>Add device to iommu  driver handling</td>
      <td>管理dev集合</td>
    </tr>
    <tr>
      <td>release_device</td>
      <td>Remove device from  iommu driver handling</td>
      <td>管理dev集合</td>
    </tr>
    <tr>
      <td>probe_finalize</td>
      <td>Do final setup work  after the device is added to an IOMMU group and attached to the groups domain</td>
      <td>管理dev集合</td>
    </tr>
    <tr>
      <td>device_group</td>
      <td>find iommu group for a  particular device</td>
      <td>管理dev集合</td>
    </tr>
    <tr>
      <td>domain_get_attr</td>
      <td>Query domain attributes</td>
      <td>管理dev集合</td>
    </tr>
    <tr>
      <td>domain_set_attr</td>
      <td>Change domain  attributes</td>
      <td>管理dev集合</td>
    </tr>
    <tr>
      <td>support_dirty_log</td>
      <td>Check whether domain  supports dirty log tracking</td>
      <td>iommu page table操作</td>
    </tr>
    <tr>
      <td>switch_dirty_log</td>
      <td>Perform actions to  start|stop dirty log tracking</td>
      <td>iommu page table操作</td>
    </tr>
    <tr>
      <td>sync_dirty_log</td>
      <td>Sync dirty log from  IOMMU into a dirty bitmap</td>
      <td>iommu page table操作</td>
    </tr>
    <tr>
      <td>clear_dirty_log</td>
      <td>Clear dirty log of  IOMMU by a mask bitmap</td>
      <td>iommu page table操作</td>
    </tr>
    <tr>
      <td>get_resv_regions</td>
      <td>Request list of  reserved regions for a device</td>
      <td>iova预留</td>
    </tr>
    <tr>
      <td>put_resv_regions</td>
      <td>Free list of reserved  regions for a device</td>
      <td>iova预留</td>
    </tr>
    <tr>
      <td>apply_resv_region</td>
      <td>Temporary helper  call-back for iova reserved ranges</td>
      <td>iova预留</td>
    </tr>
    <tr>
      <td>domain_window_enable</td>
      <td>Configure and enable a  particular window for a domain</td>
      <td>NA</td>
    </tr>
    <tr>
      <td>domain_window_disable</td>
      <td>Disable a particular  window for a domain</td>
      <td>NA</td>
    </tr>
    <tr>
      <td>of_xlate</td>
      <td>add OF master IDs to  iommu grouping</td>
      <td>NA</td>
    </tr>
    <tr>
      <td>is_attach_deferred</td>
      <td>Check if domain attach  should be deferred from iommu driver init to device driver init (default no)</td>
      <td>管理dev集合</td>
    </tr>
    <tr>
      <td>dev_has/enable/disable_feat</td>
      <td>per device entries to  check/enable/disable iommu specific features.</td>
      <td>管理dev集合</td>
    </tr>
    <tr>
      <td>dev_feat_enabled</td>
      <td>check enabled feature</td>
      <td>iommu能力check</td>
    </tr>
    <tr>
      <td>aux_attach/detach_dev</td>
      <td>aux-domain specific  attach/detach entries.</td>
      <td>aux</td>
    </tr>
    <tr>
      <td>aux_get_pasid</td>
      <td>get the pasid given an  aux-domain</td>
      <td>aux</td>
    </tr>
    <tr>
      <td>sva_bind</td>
      <td>Bind process address  space to device</td>
      <td>sva</td>
    </tr>
    <tr>
      <td>sva_unbind</td>
      <td>Unbind process address  space from device</td>
      <td>sva</td>
    </tr>
    <tr>
      <td>sva_get_pasid</td>
      <td>Get PASID associated to  a SVA handle</td>
      <td>sva</td>
    </tr>
    <tr>
      <td>page_response</td>
      <td>handle page request  response</td>
      <td>sva</td>
    </tr>
    <tr>
      <td>cache_invalidate</td>
      <td>invalidate translation  caches</td>
      <td>sva vfio</td>
    </tr>
    <tr>
      <td>sva_bind_gpasid</td>
      <td>bind guest pasid and mm</td>
      <td>sva vfio</td>
    </tr>
    <tr>
      <td>sva_unbind_gpasid</td>
      <td>unbind guest pasid and  mm</td>
      <td>sva vfio</td>
    </tr>
    <tr>
      <td>attach_pasid_table</td>
      <td>attach a pasid table</td>
      <td>sva vfio</td>
    </tr>
    <tr>
      <td>detach_pasid_table</td>
      <td>detach the pasid table</td>
      <td>sva vfio</td>
    </tr>
    <tr>
      <td>bind_guest_msi</td>
      <td>provides a stage1  giova/gpa MSI doorbell mapping</td>
      <td>NA</td>
    </tr>
    <tr>
      <td>unbind_guest_msi</td>
      <td>withdraw a stage1  giova/gpa MSI doorbell mapping</td>
      <td>NA</td>
    </tr>
    <tr>
      <td>def_domain_type</td>
      <td>device default domain type, return value</td>
      <td>管理dev集合</td>
    </tr>
  </tbody>
</table></div>

<h5 id="dirty_log的使用"><span class="me-2">dirty_log的使用</span><a href="#dirty_log的使用" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h5>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="n">HTTU</span> <span class="n">Hardware</span> <span class="n">Translation</span> <span class="n">Table</span> <span class="n">Update</span><span class="p">.</span> <span class="n">The</span> <span class="n">act</span> <span class="n">of</span> <span class="n">updating</span> <span class="n">the</span> <span class="n">Access</span> <span class="n">flag</span> <span class="n">or</span> <span class="n">Dirty</span>
<span class="n">state</span> <span class="n">of</span> <span class="n">a</span> <span class="n">page</span> <span class="n">in</span> <span class="n">a</span> <span class="n">given</span> <span class="n">TTD</span> <span class="n">which</span> <span class="n">is</span> <span class="n">automatically</span> <span class="n">done</span> <span class="n">in</span> <span class="n">hardware</span><span class="p">,</span> <span class="n">on</span> <span class="n">an</span>
<span class="n">access</span> <span class="n">or</span> <span class="n">write</span> <span class="n">to</span> <span class="n">the</span> <span class="n">corresponding</span> <span class="n">page</span><span class="p">.</span>
</pre></td></tr></tbody></table></code></div></div>

<p>参考：SMMU的spec 3.13 Translation table entries and Access/Dirty flags章节</p>

<p><a href="https://terenceli.github.io/%E6%8A%80%E6%9C%AF/2018/08/11/dirty-pages-tracking-in-migration">qemu/kvm dirty pages tracking in migration</a></p>

<p><a href="https://developer.aliyun.com/article/781810">QEMU&amp;KVM-2 Live Migration</a></p>

<p>热迁移虚拟机还是要短暂停止的，只是时间很短，影响比较小，给人感觉服务没有中断。 dirty log就是记录哪些页被修改。</p>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="n">DMA</span> <span class="n">iommu</span><span class="err">的</span><span class="n">page</span> <span class="n">write</span><span class="p">,</span> <span class="n">live</span> <span class="n">migrate</span> <span class="n">dma</span> <span class="n">dirty</span> <span class="n">log</span> <span class="n">trace</span> <span class="k">if</span> <span class="n">iommu</span> <span class="n">support</span> <span class="n">dirty</span> <span class="n">log</span><span class="o">:</span>
<span class="n">live</span> <span class="n">migrate</span> <span class="n">start</span> <span class="o">-&gt;</span>  <span class="k">switch</span> <span class="n">iommu</span> <span class="n">dirty</span> <span class="n">log</span> <span class="n">on</span> <span class="o">-&gt;</span> <span class="n">sync</span> <span class="n">dirty</span> <span class="n">log</span> <span class="n">to</span> <span class="n">bitmap</span> <span class="o">-&gt;</span> <span class="n">clear</span> <span class="n">dirty</span> <span class="n">log</span> <span class="o">-&gt;</span><span class="n">sync</span> <span class="n">to</span> <span class="n">qemu</span> <span class="n">to</span> <span class="n">copy</span> <span class="n">memory</span>
</pre></td></tr></tbody></table></code></div></div>


</div>

<div class="post-tail-wrapper text-muted">

  <!-- categories -->
  
  <div class="post-meta mb-3">
    <i class="far fa-folder-open fa-fw me-1"></i>
    
      <a href='/categories/linux/'>linux</a>,
      <a href='/categories/dma/'>DMA</a>
  </div>
  
  <!-- post.html -->

  <!-- tags -->
  
  <div class="post-tags">
    <i class="fa fa-tags fa-fw me-1"></i>
      
      <a href="/tags/dma/"
          class="post-tag no-text-decoration" >DMA</a>
      
  </div>
  
  
  <div class="post-tail-bottom
    d-flex justify-content-between align-items-center mt-3 pt-5 pb-2">
    <div class="license-wrapper">

      

        

        This post is licensed under 
        <a href="https://creativecommons.org/licenses/by/4.0/">
          CC BY 4.0
        </a>
         by the author.

      
    </div>

    <!-- Post sharing snippet -->

<div class="share-wrapper">
  <span class="share-label text-muted me-1">Share</span>
  <span class="share-icons">
    
    
    

    
      
      <a
        href="https://twitter.com/intent/tweet?text=Direct%20memory%20access(DMA)%20-%20Cheng%20Luo&url=https%3A%2F%2Fluochenglcs.github.io%2Fposts%2Fdma-and-iommu%2F"
        data-bs-toggle="tooltip"
        data-bs-placement="top"
        title="Twitter"
        target="_blank"
        rel="noopener"
        aria-label="Twitter"
      >
        <i class="fa-fw fab fa-twitter"></i>
      </a>
    
      
      <a
        href="https://www.facebook.com/sharer/sharer.php?title=Direct%20memory%20access(DMA)%20-%20Cheng%20Luo&u=https%3A%2F%2Fluochenglcs.github.io%2Fposts%2Fdma-and-iommu%2F"
        data-bs-toggle="tooltip"
        data-bs-placement="top"
        title="Facebook"
        target="_blank"
        rel="noopener"
        aria-label="Facebook"
      >
        <i class="fa-fw fab fa-facebook-square"></i>
      </a>
    
      
      <a
        href="https://t.me/share/url?url=https%3A%2F%2Fluochenglcs.github.io%2Fposts%2Fdma-and-iommu%2F&text=Direct%20memory%20access(DMA)%20-%20Cheng%20Luo"
        data-bs-toggle="tooltip"
        data-bs-placement="top"
        title="Telegram"
        target="_blank"
        rel="noopener"
        aria-label="Telegram"
      >
        <i class="fa-fw fab fa-telegram"></i>
      </a>
    

    <i
      id="copy-link"
      class="fa-fw fas fa-link small"
      data-bs-toggle="tooltip"
      data-bs-placement="top"
      title="Copy link"
      data-title-succeed="Link copied successfully!"
    >
    </i>
  </span>
</div>


  </div><!-- .post-tail-bottom -->

</div><!-- div.post-tail-wrapper -->


      
    
      
    </div>
  </div>
  <!-- #core-wrapper -->

  <!-- panel -->
  <div id="panel-wrapper" class="col-xl-3 ps-2 text-muted">
    <div class="access">
      <!-- Get the last 5 posts from lastmod list. -->














  <div id="access-lastmod" class="post">
    <div class="panel-heading">Recently Updated</div>
    <ul class="post-content list-unstyled ps-0 pb-1 ms-1 mt-2">
      
        
        
        
        <li class="text-truncate lh-lg">
          <a href="/posts/%E5%86%85%E5%AD%98%E6%95%B0%E6%8D%AE%E5%BA%93/">MMDB (memory database)</a>
        </li>
      
        
        
        
        <li class="text-truncate lh-lg">
          <a href="/posts/userspace-rcu%E9%94%81/">userspace rcu lock</a>
        </li>
      
        
        
        
        <li class="text-truncate lh-lg">
          <a href="/posts/Real-time-operating-system/">Realtime Operating System</a>
        </li>
      
        
        
        
        <li class="text-truncate lh-lg">
          <a href="/posts/HUNDSUN-LightOS/">Hundsun Lightos</a>
        </li>
      
        
        
        
        <li class="text-truncate lh-lg">
          <a href="/posts/linux-struct-folio/">struct folio</a>
        </li>
      
    </ul>
  </div>
  <!-- #access-lastmod -->


      <!-- The trending tags list -->















  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
      
        
        

  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
      
        
        

  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
      
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        



  <div id="access-tags">
    <div class="panel-heading">Trending Tags</div>
    <div class="d-flex flex-wrap mt-3 mb-1 me-3">
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/lock/">lock</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/libumem/">libumem</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/mm/">mm</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/autonuma/">autonuma</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/bfs/">BFS</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/bus/">Bus</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/caladan/">caladan</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/damon/">damon</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/deep-learning/">Deep Learning</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/dma/">DMA</a>
      
    </div>
  </div>


    </div>

    
      
      



  <div id="toc-wrapper" class="ps-0 pe-4 mb-5">
    <div class="panel-heading ps-3 pt-2 mb-2">Contents</div>
    <nav id="toc"><ul><li>Direct memory access(DMA)<ul><li><a href="#一dma">一、DMA</a></li><li><a href="#二iommu">二、IOMMU</a></li><li><a href="#三dma-iommu">三、DMA-IOMMU</a><ul><li><a href="#1dma-ops接口">1、dma ops接口</a></li><li><a href="#2iommu-ops">2、iommu ops</a><ul><li><a href="#dirty_log的使用">dirty_log的使用</a></li></ul></li></ul></li></ul></li></ul></li></ul></nav>
  </div>
  


    
  </div>
</div>

<!-- tail -->

  <div class="row">
    <div id="tail-wrapper" class="col-12 col-lg-11 col-xl-9 px-3 pe-xl-4 mt-5">
      
        
        <!--
  Recommend the other 3 posts according to the tags and categories of the current post,
  if the number is not enough, use the other latest posts to supplement.
-->

<!-- The total size of related posts -->


<!-- An random integer that bigger than 0 -->


<!-- Equals to TAG_SCORE / {max_categories_hierarchy} -->








  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  
    
  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  






<!-- Fill with the other newlest posts -->





  <div id="related-posts" class="mb-2 mb-sm-4">
    <h3 class="pt-2 mb-4 ms-1" data-toc-skip>
      Further Reading
    </h3>
    <div class="row row-cols-1 row-cols-md-2 row-cols-xl-3 g-4 mb-4">
      
        
        
        <div class="col">
          <a href="/posts/linux-kcsan/" class="card post-preview h-100">
            <div class="card-body">
              <!--
  Date format snippet
  See: ${JS_ROOT}/utils/locale-dateime.js
-->





<em
  class="small"
  data-ts="1599782400"
  data-df="ll"
  
>
  Sep 11, 2020
</em>

              <h4 class="pt-0 my-2" data-toc-skip>The Kernel Concurrency Sanitizer (KCSAN)</h4>
              <div class="text-muted small">
                <p>
                  





                  1 kcasan

2 使用

                </p>
              </div>
            </div>
          </a>
        </div>
      
        
        
        <div class="col">
          <a href="/posts/linux-kprobe/" class="card post-preview h-100">
            <div class="card-body">
              <!--
  Date format snippet
  See: ${JS_ROOT}/utils/locale-dateime.js
-->





<em
  class="small"
  data-ts="1600041600"
  data-df="ll"
  
>
  Sep 14, 2020
</em>

              <h4 class="pt-0 my-2" data-toc-skip>Kernel Probe (kprobe)</h4>
              <div class="text-muted small">
                <p>
                  





                  1 kprobe

2 debugfs kprobe trace
kprobe trace 使用debugfs的trace功能
 p[:[GRP/]EVENT] [MOD:]SYM[+offs]|MEMADDR [FETCHARGS]  : Set a probe
 r[MAXACTIVE][:[GRP/]EVENT] [MOD:]SYM[+0] [FETCHARGS]  : Set a r...
                </p>
              </div>
            </div>
          </a>
        </div>
      
        
        
        <div class="col">
          <a href="/posts/linux-userfaultfd/" class="card post-preview h-100">
            <div class="card-body">
              <!--
  Date format snippet
  See: ${JS_ROOT}/utils/locale-dateime.js
-->





<em
  class="small"
  data-ts="1600905600"
  data-df="ll"
  
>
  Sep 24, 2020
</em>

              <h4 class="pt-0 my-2" data-toc-skip>Kernel userfaultfd</h4>
              <div class="text-muted small">
                <p>
                  





                  Userfault只支持匿名页，hugetlb、共享内存；

一、软件流程

1 初始化
调用__NR_userfaultfd syscall初始化
调用syscall初始化建立匿名inode文件，并初始化file-&amp;gt;private_data,并返回用户态文件fd。


  
    用户态：

    uffd = syscall(__NR_userfaultfd, O_CLOEXE...
                </p>
              </div>
            </div>
          </a>
        </div>
      
    </div>
    <!-- .card-deck -->
  </div>
  <!-- #related-posts -->


      
        
        <!-- Navigation buttons at the bottom of the post. -->

<div class="post-navigation d-flex justify-content-between">
  
    <a
      href="/posts/linux-hugepages/"
      class="btn btn-outline-primary"
      prompt="Older"
    >
      <p>HugePages</p>
    </a>
  

  
    <a
      href="/posts/High-Performance-OS-caladan/"
      class="btn btn-outline-primary"
      prompt="Newer"
    >
      <p>High Performance OS - caladan</p>
    </a>
  
</div>

      
        
        <!--  The comments switcher -->


      
    </div>
  </div>


        <!-- The Search results -->

<div id="search-result-wrapper" class="d-flex justify-content-center unloaded">
  <div class="col-11 post-content">
    <div id="search-hints">
      <!-- The trending tags list -->















  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
      
        
        

  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
      
        
        

  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
      
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        



  <div id="access-tags">
    <div class="panel-heading">Trending Tags</div>
    <div class="d-flex flex-wrap mt-3 mb-1 me-3">
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/lock/">lock</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/libumem/">libumem</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/mm/">mm</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/autonuma/">autonuma</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/bfs/">BFS</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/bus/">Bus</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/caladan/">caladan</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/damon/">damon</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/deep-learning/">Deep Learning</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/dma/">DMA</a>
      
    </div>
  </div>


    </div>
    <div id="search-results" class="d-flex flex-wrap justify-content-center text-muted mt-3"></div>
  </div>
</div>

      </div>
    </div>

    <!-- The Footer -->

<footer>
  <div class="container px-lg-4">
    <div class="d-flex justify-content-center align-items-center text-muted mx-md-3">
      <p>Using the <a href="https://jekyllrb.com" target="_blank" rel="noopener">Jekyll</a> theme <a href="https://github.com/cotes2020/jekyll-theme-chirpy" target="_blank" rel="noopener">Chirpy</a>
      </p>

      <p>©
        2023
        <a href="https://gitee.com/luochenglcs">luochunsheng</a>.
        
          <span
            data-bs-toggle="tooltip"
            data-bs-placement="top"
            title="Except where otherwise noted, the blog posts on this site are licensed under the Creative Commons Attribution 4.0 International (CC BY 4.0) License by the author."
          >Some rights reserved.</span>
        
      </p>
    </div>
  </div>
</footer>


    <div id="mask"></div>

    <button id="back-to-top" aria-label="back-to-top" class="btn btn-lg btn-box-shadow">
      <i class="fas fa-angle-up"></i>
    </button>

    
      <div
        id="notification"
        class="toast"
        role="alert"
        aria-live="assertive"
        aria-atomic="true"
        data-bs-animation="true"
        data-bs-autohide="false"
      >
        <div class="toast-header">
          <button
            type="button"
            class="btn-close ms-auto"
            data-bs-dismiss="toast"
            aria-label="Close"
          ></button>
        </div>
        <div class="toast-body text-center pt-0">
          <p class="px-2 mb-3">A new version of content is available.</p>
          <button type="button" class="btn btn-primary" aria-label="Update">
            Update
          </button>
        </div>
      </div>
    

    <!-- JS selector for site. -->

<!-- commons -->



<!-- layout specified -->


  

  
    <!-- image lazy-loading & popup & clipboard -->
    
  















  
    

    

  

  
    

    

  

  
    

    

  

  
    

    

  

  
    

    

  

  
    

    

  

  
    

    

  

  
    

    

  

  
    

    

  

  
    

    

  

  
    

    

  



  <script src="https://cdn.jsdelivr.net/combine/npm/jquery@3.7.0/dist/jquery.min.js,npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js,npm/simple-jekyll-search@1.10.0/dest/simple-jekyll-search.min.js,npm/lazysizes@5.3.2/lazysizes.min.js,npm/magnific-popup@1.1.0/dist/jquery.magnific-popup.min.js,npm/clipboard@2.0.11/dist/clipboard.min.js,npm/dayjs@1.11.7/dayjs.min.js,npm/dayjs@1.11.7/locale/en.min.js,npm/dayjs@1.11.7/plugin/relativeTime.min.js,npm/dayjs@1.11.7/plugin/localizedFormat.min.js,npm/tocbot@4.21.0/dist/tocbot.min.js"></script>






<script defer src="/assets/js/dist/post.min.js"></script>






    

    <!--
  Jekyll Simple Search loader
  See: <https://github.com/christian-fei/Simple-Jekyll-Search>
-->





<script>
  /* Note: dependent library will be loaded in `js-selector.html` */
  SimpleJekyllSearch({
    searchInput: document.getElementById('search-input'),
    resultsContainer: document.getElementById('search-results'),
    json: '/assets/js/data/search.json',
    searchResultTemplate: '<div class="px-1 px-sm-2 px-lg-4 px-xl-0">  <a href="{url}">{title}</a>  <div class="post-meta d-flex flex-column flex-sm-row text-muted mt-1 mb-1">    {categories}    {tags}  </div>  <p>{snippet}</p></div>',
    noResultsText: '<p class="mt-5"></p>',
    templateMiddleware: function(prop, value, template) {
      if (prop === 'categories') {
        if (value === '') {
          return `${value}`;
        } else {
          return `<div class="me-sm-4"><i class="far fa-folder fa-fw"></i>${value}</div>`;
        }
      }

      if (prop === 'tags') {
        if (value === '') {
          return `${value}`;
        } else {
          return `<div><i class="fa fa-tag fa-fw"></i>${value}</div>`;
        }
      }
    }
  });
</script>

  </body>
</html>

